﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using L.M.System.Application.IServices;
using L.M.System.Domain.DTO;
using L.M.System.Domain.Entity;
using L.M.System.Infrastruecure.IRepository;
using L.M.System.Infrastruecure.Repository;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace L.M.System.Application.Services
{
    /// <summary>
    /// 用户和角色关系比
    /// </summary>
    public class Employee_role_tableServices : BaseServices<employee_role_table, string>, IEmployee_role_tableServices
    {
        private readonly IEmployee_role_tableRepository employee_Role_TableRepository;
        private readonly IStaff_tableServices staff_TableServices;

        public Employee_role_tableServices(IEmployee_role_tableRepository employee_Role_TableRepository, IStaff_tableServices staff_TableServices) : base(employee_Role_TableRepository)
        {
            this.employee_Role_TableRepository = employee_Role_TableRepository;
            this.staff_TableServices = staff_TableServices;
        }

        /// <summary>
        /// 清空用户的角色
        /// </summary>
        /// <param name="eId">用户编号</param>
        /// <returns>清空用户的角色</returns>
        public async Task<ApiReturn<string>> DelEmpInfor(string? eId)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var res = 0;
            var list = employee_Role_TableRepository.GetShowInfor(c => c.employee_number == eId);
            if (list.Count() != 0)
            {
                await employee_Role_TableRepository.DelInfor_Batch_Async(list);
            }
            if (res > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "清空成功";
            }
            else
            {
                apiReturn.Code = 200;
                apiReturn.Message = "清空成功";
            }
            return apiReturn;
        }

        /// <summary>
        /// 批量清空
        /// </summary>
        /// <param name="empinfor_RoleDTOs"></param>
        /// <returns>批量清空</returns>
        public async Task<ApiReturn<string>> DelInforAll(List<Empinfor_roleDTO> empinfor_RoleDTOs)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            List<employee_role_table> employee_Role_ = new List<employee_role_table>();
            foreach (var item in empinfor_RoleDTOs)
            {
                employee_Role_.AddRange(await employee_Role_TableRepository.GetShowInforAsync(c => c.employee_number == item.Eid));
            }
            var res =  await employee_Role_TableRepository.DelInfor_Batch_Async(employee_Role_);
            if (res > 0)
            {
                apiReturn.Code = 200;
                apiReturn.Message = "清空成功";
            }
            else
            {
                apiReturn.Code = 200;
                apiReturn.Message = "清空成功";
            }
            return apiReturn;
        }


        /// <summary>
        /// 获取该角色下所有的用户编号
        /// </summary>
        /// <returns>获取该角色下所有的用户编号</returns>
        public async Task<ApiReturn<string>> GetEnumInfor(string? rId)
        {
            var eid = "";
            var list = await employee_Role_TableRepository.GetShowInforAsync(c => c.role_number == rId);
            foreach (var item in list)
            {
                eid += item.employee_number + ",";
            }
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            apiReturn.Code = 200;
            apiReturn.Message = eid.TrimEnd(',');
            return apiReturn;
        }

        /// <summary>
        /// 获取该角色下所有的用户名称
        /// </summary>
        /// <returns>获取该角色下所有的用户编号</returns>
        public async Task<ApiReturn<string>> GetEnumInforName(string? rId)
        {
            var ename = "";
            var list = await employee_Role_TableRepository.GetShowInforAsync(c => c.role_number == rId);
            foreach (var item in list)
            {
                ename += staff_TableServices.GetShowObj(c => c.employee_number == item.employee_number).employee_name + ",";
            }
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            apiReturn.Code = 200;
            apiReturn.Message = ename.TrimEnd(',');
            return apiReturn;
        }


        /// <summary>
        /// 删除用户信息 并删除关联表
        /// </summary>
        /// <param name="sId">编号</param>
        /// <returns>统一返回格式</returns>

        public async Task<ApiReturn<string>> StaffDelInfor(string? sId)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var res = await staff_TableServices.GetShowObjAsync(c => c.employee_number == sId);
            if (res != null)
            {
                var ges = await staff_TableServices.DelInfor_Async(res);
                //删除用户关联的角色
                var list = (await employee_Role_TableRepository.GetShowQueryableAsync(c => c.employee_role_number == sId)).ToList();
                if(list.Count() != 0)
                {
                    await employee_Role_TableRepository.DelInfor_Batch_Async(list);
                }
                if (ges > 0)
                {
                    apiReturn.Code = 200;
                    apiReturn.Message = "删除成功";
                }
                else
                {
                    apiReturn.Code = 400;
                    apiReturn.Message = "修改失败";
                }
            }
            else
            {
                apiReturn.Code = 400;
                apiReturn.Message = "清先选择要删除的人员";
            }
            return apiReturn;
        }
    }
}
